package com.ymttest.database.redis;

import java.util.Set;

import com.ymt.core.tool.Logger;
import com.ymt.database.RedisDriver;
import com.ymttest.utils.EnvSetup;

public class ImWapper {
	public RedisDriver rd;
	int groupid = 1;

	public ImWapper() {
		rd = new RedisDriver("group" + groupid,
				EnvSetup.getSystemUrl("sentinel"));
	}

	private void setSellerId(int sellerid) {
		groupid = sellerid % 4 + 1;
		rd = new RedisDriver("group" + groupid,
				EnvSetup.getSystemUrl("sentinel"));
	}

	public Set<String> getLastMs(int sellerid, int limit) {
		setSellerId(sellerid);
		return rd.getLastZRange("Ms:" + sellerid, limit);

	}

	public String getLastMs(int sellerid) {
		setSellerId(sellerid);
		return rd.getLastZRange("Ms:" + sellerid, 1).iterator().next();
	}

	public Set<String> getLastUnMs(int sellerid, int limit) {
		setSellerId(sellerid);
		Set<String> data = rd.getLastZRange("UnMs:" + sellerid, limit);
		return data;
	}

	public String getLastUnMs(int sellerid) {
		setSellerId(sellerid);
		return rd.getLastZRange("UnMs:" + sellerid, 1).iterator().next();
	}

	public String getCount(int sellerid,int userid) {
		setSellerId(sellerid);
		//return rd.getValue("IMCache:11:" + sellerid + ":" + "1");
		//IMCache:11:1007_3791814:1
		String count = rd.getValue("IMCache:11:"+sellerid+"_"+userid+":"+"1");
		return count;
	}

	public static void main(String args[]) {
		ImWapper imw = new ImWapper();
		 Logger.debug(imw.getLastMs(3383, 10));
		// 在FromUserId%4哨兵namespace下增加key：IMCache:11:FromUserId:1
		// 在ToUserId%4哨兵namespace下增加key：IMCache:11:ToUserId:1
		 String unms = imw.getLastUnMs(288484);
		 Logger.debug(unms);
		// Logger.debug(last);
//		Logger.debug(imw.getCount(390572,3791815));
		Set<String> set = imw.rd.keys("IMCache:11*");
		for (String s : set) {
			Logger.debug(s);
		}
	}
}
